/*
 *   Time Tracking Web Application
 *   Copyright (C) 2008, Nick Griffiths
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Contact nicobrevin at gmail dot com
 *   http://code.google.com/p/openharvest/
 */

package org.brevs.time.manager;

import java.util.Date;
import java.util.List;

import org.appfuse.model.User;
import org.brevs.time.dao.ProjectDao;
import org.brevs.time.dao.TimeDao;
import org.brevs.time.model.Entry;
import org.brevs.time.model.Project;
import org.brevs.time.model.ProjectCategory;
import org.springframework.beans.factory.InitializingBean;

public class TimeManagerImpl implements TimeManager, InitializingBean {
	
	private TimeDao timeDao;
	private ProjectDao projectDao;
	
	public void setTimeDao(TimeDao timeDao) {
		this.timeDao = timeDao;
	}
	
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	public void saveEntry(Entry entry) {
		if (entry.getStartDate() == null) {
			entry.setStartDate(new Date());
		}

		timeDao.saveEntry(entry);
	}
	
	public List<Entry> getEntries(Date day, User user) {
		return timeDao.getEntries(user, day);
	}
	
	public void startTiming(Entry entry) {
		
		// make sure all the others have stopped (there should only be one)
		List<Entry> runningEntries = timeDao.getEntries(entry.getUser(), true);
		for (Entry runningEntry : runningEntries) {
			stopTiming(runningEntry);
		}
		
		if (entry.getStartedRunning() == null) {
			entry.setStartedRunning(new Date());
			saveEntry(entry);
		} else {
			throw new RuntimeException("timer entry is already running" + entry);
		}
	}
	
	public Entry getEntry(Long id) {
		return timeDao.getEntry(id);
	}
	
	public void stopTiming(Entry entry) {
		Date started = entry.getStartedRunning();
		Date now = new Date();
		long timeDiff = now.getTime() - started.getTime();
		int secondsDiff = millisToSecs(timeDiff);
		entry.setSeconds(entry.getSeconds() + secondsDiff);
		entry.setStartedRunning(null);
		saveEntry(entry);
	}

	private int millisToSecs(long timeDiff) {
		return (int) (timeDiff / 1000);
	}

	public void afterPropertiesSet() throws Exception {
		
	}
	
	public List<Project> getProjects(User user) {
		return projectDao.getAllForUser(user);
	}
	
	public Project getProject(Long id) {
		return projectDao.getProject(id);
	}
	
	public ProjectCategory getProjectCategory(Long id) {
		return projectDao.getProjectCategory(id);
	}
	
	public void createDemoData() {
		Project project = new Project();
		project.setName("Test Project");
		project.addNewCategory("Work");
		project.addNewCategory("Rest");
		project.addNewCategory("Play");
		timeDao.saveProject(project);
		
		project = new Project();
		project.setName("Great Project");
		project.addNewCategory("Eat");
		project.addNewCategory("Drink");
		project.addNewCategory("Sleep");
		timeDao.saveProject(project);
		
	}
	
	public String helloWorld() {
		return "hello world";
	}
}
